package etcddriver

import (
	"context"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"

	"github.com/spf13/viper"
)

const (
	DEFAULT_DIAL_TIME_OUT = 10
	DEFAULT_REQ_TIME_OUT  = 10
)

func init() {
	depend.Provide(NewETCDConfig)
	depend.Provide(NewETCDDriver)
}

const (
	keyEtcdConfig = driver.KeyDriverPerfix + `etcd`
)

type EtcdConfig struct {
	EntryPoint []string `mapstructure:"entry_point"`
}

func NewETCDConfig(vp *viper.Viper) (*EtcdConfig, error) {
	cfg := new(EtcdConfig)
	if err := vp.UnmarshalKey(keyEtcdConfig, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

func NewETCDDriver(cfg *EtcdConfig) (*EtcdDriver, error) {
	return NewEtcdDriver(
		context.Background(),
		DEFAULT_DIAL_TIME_OUT,
		DEFAULT_REQ_TIME_OUT,
		cfg.EntryPoint,
	)
}
